Warning: mkdir(): No space left on device in /var/www/tg-me/post.php on line 37

Warning: file_put_contents(aCache/aDaily/post/defront/--): Failed to open stream: No such file or directory in /var/www/tg-me/post.php on line 50
Defront — про фронтенд-разработку и не только | Telegram Webview: defront/1113 -
Telegram Group & Telegram Channel
Неожиданное пересечение типов в TypeScript

Стэфан Баумгартнер рассказал, в каких случаях могут возникать неожиданные пересечения типов, и что с ними делать — "TypeScript: Unexpected intersections".

Неожиданное пересечение типов возникает при обновлении свойств объектов, если в качестве ключа используется переменная с объединением типов. В этом случае для ключа TypeScript будет использовать "наибольший общий делитель" в виде пересечения типов. В примере ниже таким "делителем" будет пересечение string & number, то есть тип never, из-за чего возникнет ошибка типизации:

interface Person {
age: number;
name: string;
}

function updatePersonByKey(
person: Person,
key: keyof Person,
value: Person[keyof Person]
) {
// тут будет ошибка типизации, так как
// key превратится в пересечение типов `string & number`
person[key] = value;
}

Также неожиданное пересечение типов может возникнуть после деструктуризации объекта, так как деструктурированные свойства теряют связь с оригинальным типом.

Полезная статья. Рекомендую почитать всем, кто использует TypeScript.

#typescript

https://fettblog.eu/typescript-unexpected-intersections/



tg-me.com/defront/1113
Create:
Last Update:

Неожиданное пересечение типов в TypeScript

Стэфан Баумгартнер рассказал, в каких случаях могут возникать неожиданные пересечения типов, и что с ними делать — "TypeScript: Unexpected intersections".

Неожиданное пересечение типов возникает при обновлении свойств объектов, если в качестве ключа используется переменная с объединением типов. В этом случае для ключа TypeScript будет использовать "наибольший общий делитель" в виде пересечения типов. В примере ниже таким "делителем" будет пересечение string & number, то есть тип never, из-за чего возникнет ошибка типизации:


interface Person {
age: number;
name: string;
}

function updatePersonByKey(
person: Person,
key: keyof Person,
value: Person[keyof Person]
) {
// тут будет ошибка типизации, так как
// key превратится в пересечение типов `string & number`
person[key] = value;
}

Также неожиданное пересечение типов может возникнуть после деструктуризации объекта, так как деструктурированные свойства теряют связь с оригинальным типом.

Полезная статья. Рекомендую почитать всем, кто использует TypeScript.

#typescript

https://fettblog.eu/typescript-unexpected-intersections/

BY Defront — про фронтенд-разработку и не только




Share with your friend now:
tg-me.com/defront/1113

View MORE
Open in Telegram


Defront при поддержке Зарплата ру — про фронтенд разработку и не только Telegram | DID YOU KNOW?

Date: |

What Is Bitcoin?

Bitcoin is a decentralized digital currency that you can buy, sell and exchange directly, without an intermediary like a bank. Bitcoin’s creator, Satoshi Nakamoto, originally described the need for “an electronic payment system based on cryptographic proof instead of trust.” Each and every Bitcoin transaction that’s ever been made exists on a public ledger accessible to everyone, making transactions hard to reverse and difficult to fake. That’s by design: Core to their decentralized nature, Bitcoins aren’t backed by the government or any issuing institution, and there’s nothing to guarantee their value besides the proof baked in the heart of the system. “The reason why it’s worth money is simply because we, as people, decided it has value—same as gold,” says Anton Mozgovoy, co-founder & CEO of digital financial service company Holyheld.

The SSE was the first modern stock exchange to open in China, with trading commencing in 1990. It has now grown to become the largest stock exchange in Asia and the third-largest in the world by market capitalization, which stood at RMB 50.6 trillion (US$7.8 trillion) as of September 2021. Stocks (both A-shares and B-shares), bonds, funds, and derivatives are traded on the exchange. The SEE has two trading boards, the Main Board and the Science and Technology Innovation Board, the latter more commonly known as the STAR Market. The Main Board mainly hosts large, well-established Chinese companies and lists both A-shares and B-shares.

Defront при поддержке Зарплата ру — про фронтенд разработку и не только from tw


Telegram Defront — про фронтенд-разработку и не только
FROM USA